package news.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.ViewUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import base.BaseFragment;
import base.HMBaseAdapter;
import shanshuitiancheng.news.R;

/**
 * Created by cch on 2016/6/17.
 */
public class MenuFragment2 extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView lv1;
    private ListView lv2;
    private ListView lv3;
    private MenuAdapter adapter;


    @Override
    public View initView(LayoutInflater inflater) {
        //初始化控件
        View view = inflater.inflate(R.layout.layout_left_menu, null);
        TextView tv = (TextView) view.findViewById(R.id.tv_menu_classify);
        lv1 = (ListView) view.findViewById(R.id.lv_menu_news_center);
        lv2 = (ListView) view.findViewById(R.id.lv_menu_smart_service);
        lv3 = (ListView) view.findViewById(R.id.lv_menu_govaffairs);

        lv1.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    private List<String> menulist = new ArrayList<String>();
    private View view;

    public void initMenu(List<String> menuNewCenterList) {
        menulist.clear();//清理下里面的数据，确保里面没有任何杂质，以确定传过来的数据是准确的.
        menulist.addAll(menuNewCenterList);
        //判断adapter中是否有我们要添加的信息，如果是空的直接添加，如果有其他的信息的话
        //用notifyDataSetChanged方法来通知如果适配器的内容改变时需要强制调用getView来刷新每个Item的内容。
        //notifyDataSetChanged()，重绘当前可见区域
        if (adapter == null) {
            adapter = new MenuAdapter(ct, menulist);
            lv1.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.setCurPostion(0);//初始化当你进去滑动

       /* adapter = new MenuAdapter(ct,menulist);//没有判断之前的代码
         lv1.setAdapter(adapter);*/
    }

//点击条目的位置时候自动跳转
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        adapter.setCurPostion(position);
        sm.toggle();
    }

// 现在我们封装了一个黑马高大上的adapter


    class MenuAdapter extends HMBaseAdapter {
        private int curPostion = 0;//当前选中的位置


        public MenuAdapter(Context ct, List<String> menulist) {
            super(ct, menulist);
        }

        public void setCurPostion(int postion) {
            this.curPostion = postion;
            //notifyDataSetChanged()，重绘当前可见区域
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = view.inflate(ct,R.layout.layout_item_menu,null);
//                convertView = view.inflate(ct, R.layout.layout_item_menu, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.tv_menu_item);
            ImageView image = (ImageView) convertView.findViewById(R.id.iv_menu_item);
            tv.setText(menulist.get(position));
            //如果当前选中的位置就是设置为红，没有选中的就设置为白色
            if (curPostion == position) {
                tv.setTextColor(getResources().getColor(R.color.red));
                image.setBackgroundResource(R.mipmap.menu_arr_select);
                convertView.setBackgroundResource(R.drawable.menu_item_bg_select);//设置点击时的背景颜色跟条纹
            } else {
                tv.setTextColor(getResources().getColor(R.color.white));
                image.setBackgroundResource(R.mipmap.menu_arr_normal);
                convertView.setBackgroundResource(R.drawable.transparent);
            }
            Log.i("fuck终于可以了","this is fuck day +"+ convertView);
            return convertView;
        }

//这个是最基本的初始化adapter的数据。。

//    private class MenuAdapter extends BaseAdapter{
//            private Context ct;
//        private List<String> menuList;
//        //当前选中的位置
//        private int curPostion = 0;
//        public MenuAdapter(Context ct, List<String> menulist) {
//            this.ct = ct;
//            this.menuList = menulist;
//        }
//        public  void setCurPostion(int postion){
//                this.curPostion = postion;
//            //notifyDataSetChanged()，重绘当前可见区域
//                notifyDataSetChanged();
//        }
//        @Override
//        public int getCount() {
//            return menuList.size();
//        }
//
//        @Override
//        public Object getItem(int position) {
//            return menuList.get(position);
//        }
//
//        @Override
//        public long getItemId(int position) {
//            return position;
//        }
//
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            if (convertView == null){
//                convertView = view.inflate(ct,R.layout.layout_item_menu,null);
//            }
//           TextView tv = (TextView) convertView.findViewById(R.id.tv_menu_item);
//            ImageView image = (ImageView) convertView.findViewById(R.id.iv_menu_item);
//            tv.setText(menuList.get(position));
//            if (curPostion == position){
//                tv.setTextColor(getResources().getColor(R.color.red));
//                image.setBackgroundResource(R.mipmap.menu_arr_select);
//                convertView.setBackgroundResource(R.drawable.menu_item_bg_select);//设置点击时的背景颜色跟条纹
//            }else {
//                tv.setTextColor(getResources().getColor(R.color.white));
//                image.setBackgroundResource(R.mipmap.menu_arr_normal);
//                convertView.setBackgroundResource(R.drawable.transparent);
//            }
//            return convertView;
//        }
//    }
    }
}
